/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @filename: unique.h
 */

#ifndef ___UNIQUE_H__
#define ___UNIQUE_H__
#include <rb_tree.h>
#include<sql_alloc.h>
/*
   Unique -- class for unique (removing of duplicates).
   Puts all values to the TREE. If the tree becomes too big,
   it's dumped to the file ???. User can request sorted values, or
   just iterate through them. In the last case tree merging is performed in
   memory simultaneously with iteration, so it should be ~2-3x faster.
 */
class Table;
class Unique :public Sql_alloc
{
  /// Todo
  DYNAMIC_ARRAY file_ptrs;

  ulong  max_elements;


  size_t max_in_memory_size;

  IO_CACHE file;

  TREE tree;

  unsigned char *record_pointers;

  bool flush();

  uint32_t size;

public:
  ulong elements;

  Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
	       uint32_t size_arg, size_t max_in_memory_size_arg);

  ~Unique();

  inline ulong elements_in_tree() { return tree.elements_in_tree; }

  inline bool unique_add(void *ptr)
  {
    if (tree.elements_in_tree > max_elements && flush())
      return(1);
    return(!::tree_insert(&tree, ptr, 0, tree.custom_arg));
  }

  bool get(Table *table);

  static double get_use_cost(uint32_t *buffer, uint32_t nkeys, uint32_t key_size,
                             size_t max_in_memory_size);

  inline static int get_cost_calc_buff_size(ulong nkeys, uint32_t key_size,
                                            size_t max_in_memory_size)
  {
    register size_t max_elems_in_tree=
      (1 + max_in_memory_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+key_size));
    return (int) (sizeof(uint32_t)*(1 + nkeys/max_elems_in_tree));
  }

  void reset();

  bool walk(tree_walk_action action, void *walk_action_arg);

  friend int unique_write_to_file(unsigned char* key, element_count count,
                                  Unique *unique);
  friend int unique_write_to_ptrs(unsigned char* key, element_count count,
                                  Unique *unique);
};
/*==============================Back End Part ==============================*/

class Mutex  ;

class UniqueID
{
   ulong startID;

   Mutex mutex;
   UniqueID& operator=(UniqueID&);
   UniqueID (const UniqueID&);
   bool isInited;
public:
   UniqueID():startID (1) {  mutex.init(); isInited= true;}

   virtual ~UniqueID(){ mutex.destroy();}

   inline void setID(ulong id)
   {
        startID = id;
        if (!isInited)
          mutex.init();
   }


   inline ulong getID()
   {
      //TODO::change mutex to atomic increment instruction
      if (mutex.getLock(-1, false) != 0) return 0;
          startID++;
      mutex.releaseLock(-1, false);
      return startID;
   }
};

#endif /* ___UNIQUE_H__ */
